package com.chatmusicstatus.accounts;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class AccountSQLiteHelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "accounts.db";
	private static final int DATABASE_VERSION = 1;

	// Table name
	public static final String TABLE = "accounts";

	// Columns
        public static final String ID = BaseColumns._ID;
	public static final String ANAME = "AccountName";
        public static final String SERVER = "Server";
        public static final String SERVICE = "Service";
        public static final String PORT = "Port";
        public static final String UNAME = "UserName";
        public static final String PASS = "Password";
        public static final String ENABLE = "Enable";   //0:false 1:true
        public static final String MESSAGE = "message"; //if empty use system default
        public static final String UTUNE = "UserTune";  //0:Disabled 1:Enabled 2:System_Default
        
        //Column Groups used by other pages
        public static final String[] ACCOUNTDISP = {BaseColumns._ID,ANAME,ENABLE};
        public static final String[] ACCOUNTDINFO = {ANAME,SERVER,PORT,UNAME,PASS,ENABLE,MESSAGE,UTUNE,SERVICE};
        public static final String[] ACCOUNTDETAILS = {SERVER,PORT,UNAME,PASS,MESSAGE,UTUNE,SERVICE};

	public AccountSQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table " + TABLE + "( " 
                        + BaseColumns._ID + " integer primary key autoincrement, " 
                        + ANAME + " text unique not null CHECK (" + ANAME + " <> ''), "
                        + SERVER + " text not null CHECK (" + SERVER + " <> ''), "
                        + PORT + " text not null CHECK (" + PORT + " <> ''), "
                        + SERVICE + " text,"
                        + UNAME + " text not null CHECK (" + UNAME + " <> ''), "
                        + PASS + " text,"
                        + ENABLE + " integer not null,"
                        + MESSAGE + " text,"
                        + UTUNE + " integer not null);";
		db.execSQL(sql);
	}

        @Override
        public void onUpgrade(SQLiteDatabase sqld, int oldVersion, int newVersion) {
            if (oldVersion >= newVersion)
			return;

        }

        //Helper functions
        Cursor getAccInfoFromID(int acc_id) {
            SQLiteDatabase db = getReadableDatabase();
            return db.query(TABLE, ACCOUNTDINFO, ID + "=?", new String[] {Integer.toString(acc_id)}, null, null, null);
        }
        
        public Cursor getActiveAccounts() {
            SQLiteDatabase db = getReadableDatabase();
            return db.query(TABLE, ACCOUNTDETAILS, ENABLE + "=?", new String[] {Integer.toString(1)}, null, null, null);
        }
}